#!/usr/bin/env python3
from PIL import Image, ImageDraw, ImageFont
import os

def create_icon(size, filename):
    # 创建一个深色背景的图像
    img = Image.new('RGBA', (size, size), (45, 55, 72, 255))
    draw = ImageDraw.Draw(img)
    
    # 绘制终端窗口边框
    border_width = max(1, size // 32)
    draw.rectangle([border_width, border_width, size-border_width, size-border_width], 
                  outline=(26, 32, 44, 255), width=border_width)
    
    # 绘制终端内容区域
    content_margin = size // 8
    draw.rectangle([content_margin, content_margin, size-content_margin, size-content_margin], 
                  fill=(26, 32, 44, 255))
    
    # 绘制标题栏
    title_height = size // 16
    draw.rectangle([content_margin, content_margin, size-content_margin, content_margin + title_height], 
                  fill=(74, 85, 104, 255))
    
    # 绘制终端提示符 "$"
    if size >= 32:
        try:
            font_size = max(8, size // 8)
            # 尝试使用系统字体
            try:
                font = ImageFont.truetype('/System/Library/Fonts/Monaco.ttc', font_size)
            except:
                font = ImageFont.load_default()
            
            text = "$"
            # 计算文本位置
            bbox = draw.textbbox((0, 0), text, font=font)
            text_width = bbox[2] - bbox[0]
            text_height = bbox[3] - bbox[1]
            
            x = (size - text_width) // 2
            y = size // 2 + size // 8
            
            draw.text((x, y), text, fill=(0, 255, 0, 255), font=font)
        except:
            # 如果字体加载失败，绘制简单的矩形作为提示符
            prompt_size = size // 16
            x = (size - prompt_size) // 2
            y = size // 2 + size // 8
            draw.rectangle([x, y, x + prompt_size, y + prompt_size], fill=(0, 255, 0, 255))
    
    # 绘制一些终端行
    if size >= 64:
        line_height = max(1, size // 64)
        line_spacing = max(2, size // 32)
        start_y = content_margin + title_height + line_spacing
        
        for i in range(3):
            y = start_y + i * (line_height + line_spacing)
            if y + line_height < size - content_margin - size // 4:
                line_width = (size - 2 * content_margin) * (0.6 + i * 0.1)
                draw.rectangle([content_margin + size//32, y, 
                              content_margin + line_width, y + line_height], 
                              fill=(0, 255, 0, 128))
    
    img.save(filename)
    print(f"Created {filename} ({size}x{size})")

# 创建所需的图标文件
if __name__ == "__main__":
    os.makedirs('icons', exist_ok=True)
    
    # 创建不同尺寸的 PNG 图标
    create_icon(32, 'icons/32x32.png')
    create_icon(128, 'icons/128x128.png')
    create_icon(256, 'icons/128x128@2x.png')
    create_icon(32, 'icons/icon.png')
    
    print("All icons created successfully!")